<!--
  Created by IntelliJ IDEA.
  User: Dai
  Date: 2018/4/13
  Time: 9:36
  Remark: 工程配置 -  对象管理 -  项目管理 - 编辑设备树 - 批量编辑
  To change this template use File | Settings | File Templates.
-->
<!--标题栏-->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">编辑&nbsp;&gt;&nbsp;<span th:text="${projectManage.name}">???</span></h4>
</div>
<!--表格栏-->
<div class="modal-body" style="padding: 0;">
    <div style="width: 100%;min-height: 300px;">
        <!--树-->
        <div class="col-xs-4" style="padding: 0;height: 300px; border-right: 1px solid #ddd;">
            <div class="tree-title" style="width: 100%;float: none;">选择编辑对象</div>
            <div class="zTreeDemoBackground">
                <ul id="ztree_only_parent" class="ztree"></ul>
            </div>
        </div>
        <!--配置栏目-->
        <div class="col-sm-8" style="padding: 20px">
            <!--所选变流器-->
            <fieldset style="padding: 10px">
                <legend>所选变流器</legend>
                <label style="margin-right: 10px">通信类型</label>
                <select style="margin-right: 20px" id="pcs_communication">
                    <option>Modbus</option>
                    <option>CAN</option>
                </select>
                <label>配置文件</label>
                <input type="file" style="display: inline-block;" id="pcs_file">
            </fieldset>
            <!--所选电池包-->
            <fieldset style="padding: 10px; margin-top: 20px">
                <legend>所选电池包</legend>
                <label style="margin-right: 10px">通信类型</label>
                <select style="margin-right: 20px" id="bms_communication">
                    <option>Modbus</option>
                    <option>CAN</option>
                </select>
                <label>配置文件</label>
                <input type="file" style="display: inline-block;" id="bms_file">
            </fieldset>
        </div>
    </div>
</div>
<!--按钮栏-->
<div class="modal-footer">
    <button type="button" class="btn btn-default" onclick="check_info(this)">确定</button>
</div>
<!--该页面的js 未完成部分见TODO -->
<script th:inline="javascript">
    var ztree_node_only_parent = [[${ztree_node_only_parent}]];

    var setting_only_parent = {
        view: {
            showIcon: false,        // 是否显示节点的图标
        },
        check: {
            enable: true,
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",        // 节点数据中保存唯一标识的属性名称
                pIdKey: "pId",      // 节点数据中保存其父节点唯一标识的属性名称
                rootPId: ""         // 用于修正根节点父节点数据 默认值：null
            }
        }
    };

    $(document).ready(function () {
        var ztree_only_parent = $.fn.zTree.init($("#ztree_only_parent"), setting_only_parent, ztree_node_only_parent);
        ztree_only_parent.expandAll(true);    // 设置默认展开
    });

    /**
     * 检查填写的信息是否完整
     */
    function check_info(button) {
        var treeObj = $.fn.zTree.getZTreeObj("ztree_only_parent"),
            nodes = treeObj.getCheckedNodes(true),
            v = "";
        if (nodes.length<1){
            alert("请选择要编辑的对象");
            return false;
        }
        for (var i = 1; i < nodes.length; i++) {
            v += nodes[i].id + ",";
            console.log(nodes[i].type);
            if (nodes[i].type == 1){
                if (!$("#pcs_file")[0].files[0]){
                    alert("请上传变流器配置文件");
                    return false;
                }
            } else {
                if (!$("#bms_file")[0].files[0]){
                    alert("请上传电池包配置文件");
                    return false;
                }
            }
        }
        var project_id =  $("#tree_id").val();
        v = v.replace(project_id + "," , "");
        var fd = new FormData();
        fd.append("ids",v);
        fd.append("project_id", $("#tree_id").val());
        fd.append("pcs_communication", $("#pcs_communication").val());
        fd.append("bms_communication", $("#bms_communication").val());
        fd.append("pcs_file", $("#pcs_file")[0].files[0]);
        fd.append("bms_file", $("#bms_file")[0].files[0]);
        $.ajax({
            url: "save_project_edit_list",
            type: "POST",
            processData: false,
            contentType: false,
            data: fd,
            success: function (result) {
                if (result.type == "alert") {
                    alert(result.message);
                    $("#messageName").val(null);
                } else {
                    zNodes = result;
                    var treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    treeObj.expandAll(true);    // 设置默认展开
                    // 关闭弹框
                    $(button).parent().siblings(":first").find("button").click();
                }
            }
        });
    }
</script>